#!/bin/bash

# Copyright (C) Graphic Tribe. All rights reserved.

# CONFIGURATION (1):
# xabatch: set paths to config and program files.
# XabatchConfigFile=
# XabatchProgramFile=
# END CONFIGURATION (1).

while [[ $# -gt 0 ]] ; do
	case "$1" in
	-h | --help)
		;;
	-l | --logs)
		LOGS=1; args=1 ;;
	-x | --xabatch)
		XABATCH=1; args=1 ;;
	-y | --yes)
		args=1 ;;
	esac
	shift
done

if [[ $args != 1 ]] ; then
	/usr/bin/open ./BWW2.rtf; exit
fi

if [[ `stat -f %p $0` != "100550" ]] ; then
	echo "Script is not properly protected."
	echo "Script mode is `stat -f %p $0` but should be 100550."
	exit
fi
if [[ `stat -f %u%g $0` != "080" ]] ; then
	echo "Script is not properly protected."
	echo "Script ownership is `stat -f %u:%g $0` but should be 0:80."
	exit
fi

Steps=4
if [[ $XABATCH == 1  && -e "$XabatchConfigFile" && -e "$XabatchProgramFile" ]] ; then
	Steps=5
fi

echo -n "Cleansing started on ";date '+%Y.%m.%d at %H:%M:%S.'; echo "---"; echo;

echo "Step 1 of $Steps: Removing/emptying caches, logs..."
rm -rf /Library/Caches/* /private/var/folders/*/*/-Caches-/* /System/Library/Caches/* ~/Library/Caches/*
if [[ $LOGS == 1 ]] ; then
	rm -rf /Library/Logs/* ~/Library/Logs/* /private/var/log/*.log.* /private/var/log/*.log.*.bz2 /private/var/log/*/*
	find /private/var/log \( -name "*.log" -or -name "*_log" -or -name "*.out" -or -name "*.db" -or -name "lastlog" -or -name "OSInstall.custom" -or -name "wtmp" \) -exec cp /dev/null {} \;
fi
echo "Step 1 completed."; echo

echo "Step 2 of $Steps: Emptying junk files in home area..."
if [[ -e ~/.bash_history ]] ; then
	cp /dev/null ~/.bash_history
fi
if [[ -e ~/.recently-used.xbel ]]; then
	cp /dev/null ~/.recently-used.xbel
fi
rm -rf ~/.thumbnails/* ~/.fontconfig/*
echo "Step 2 completed."; echo

echo "Step 3 of $Steps: Removing localizations, scratch pad NIB files..."
if [[ -e ~/Applications ]] ; then
	UserApps=~/Applications
fi

# CONFIGURATION (2):
# find /Applications $UserApps /Library ~/Library \( -iname '*.lproj' -and \! \( -iname 'en*' \) -and \! \( -path *Acrobat* -or -path *Adobe* -or -path /Library/Receipts/* -or -path *Microsoft* \) \) -print0 | xargs -0 rm -frv {}

# Add or remove localizations you want to keep by (for example w/ German) appending " -or iname 'de*' -or -iname 'ger*' " after " -iname 'en*'".

# Example of configuration for keeping French and Japanese localizations:
# find /Applications $UserApps /Library ~/Library \( -iname '*.lproj' -and \! \( -iname 'fr*' -or -iname 'ja*' \) -and \! \( -path *Acrobat* -or -path *Adobe* -or -path /Library/Receipts/* -or -path *Microsoft* \) \) -print0 | xargs -0 rm -frv {}

# No need to modify anything below this point
# END CONFIGURATION (2).

find /Applications $UserApps /Library ~/Library -type f \( \( -name 'classes.nib' -or -name 'designable.nib' -or -name 'info.nib' \) -and \! \( -path *Acrobat* -or -path *Adobe* -or -path /Library/Receipts/* -or -path *Microsoft* \) \) -print0 | xargs -0 rm -frv {}
echo "Step 3 completed."; echo

echo "Step 4 of $Steps: Removing .DS_Store, .localized..."
if [[ -n `grep -i "Finder" <<< \`ps -A\`` ]] ; then
	DesktopPath=~/Desktop/.DS_Store
fi
find / \( \( -name '.DS_Store' -or -name '.localized' \) -and \! -path "$DesktopPath" \) -print0 | xargs -0 rm -frv {}
echo "Step 4 completed."; echo

if [[ Steps = 5 ]] ; then
	echo "Step 5 of $Steps: Removing XAs in home area:"
	cat "$XabatchConfigFile"; echo
	find ~ -print0 | xargs -0 "$XabatchProgramFile" "$XabatchConfigFile" {}
	echo "Step 5 completed."; echo
fi

echo; echo -n "Done. Cleansing completed on "; date '+%Y.%m.%d %H:%M:%S.'; echo; echo "Rebooting..."; say "Done. Rebooting."; reboot
